using System;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.Collections.Generic;
using Metro.Admin.BusinessEntities;
using Metro.Admin.DataAccess;
using Metro.Security;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation;
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Metro.CurrenciesService
{

    public class SharedService : ISharedService
    {
        public User UsersAuthentication(string login, string password)
        {
            User user= null;
            AdminDataSource userDataSource;
            string salt = string.Empty, hash = string.Empty;

            try
            {
              userDataSource = new AdminDataSource();
              user = userDataSource.GetUserByLogin(login);

              hash = Authentication.CreatePasswordHash(password, user.Salt);

              if (user != null)
              {
                  if (user.Hash != hash)
                      user = null;
              }
                  
            }
            catch (Exception ex)
            {
                LogEntry log = new LogEntry();
                log.EventId = 100;
                log.Message = ex.Message;
                log.Categories.Add("UsersAuthentication");
                log.Severity = TraceEventType.Error;
                log.Priority = 0;
                log.ProcessName = "UsersAuthentication Method";
                Logger.Write(log);
            }

            return user; 
        
        }
        public Company GetCompanyParameters()
        {
            AdminDataSource paranDataSource;
            ICollection<Parameter> list;
            Company company = null;
            Stats stats = null;
            Metro.Admin.DataAccess.Transaction TransactionDA = null; 

            try
            {
                TransactionDA = new Metro.Admin.DataAccess.Transaction();
                paranDataSource = new AdminDataSource();
                list = paranDataSource.GetParameters("F");
                company = new Company();

                foreach (Metro.Admin.BusinessEntities.Parameter param in list)
                {
                    if (param.Name.Equals("openSpot"))
                        company.OpenSpot = Convert.ToDateTime(param.Value);
                    if (param.Name.Equals("closeSpot"))
                        company.CloseSpot = Convert.ToDateTime(param.Value);
                    if (param.Name.Equals("openNextDay"))
                        company.OpenNextDay = Convert.ToDateTime(param.Value);
                    if (param.Name.Equals("closeNextDay"))
                        company.CloseNextDay = Convert.ToDateTime(param.Value);
                    if (param.Name.Equals("minPrice"))
                        company.MinimalPrice = Convert.ToDecimal(param.Value);
                    if (param.Name.Equals("maxPrice"))
                        company.MaximalPrice = Convert.ToDecimal(param.Value);
                    if (param.Name.Equals("minAmount"))
                        company.MinimalAmount = Convert.ToDecimal(param.Value);
                    if (param.Name.Equals("maxAmount"))
                        company.MaximalAmount = Convert.ToDecimal(param.Value);
                    if (param.Name.Equals("taxBase"))
                        company.TaxBase = Convert.ToDecimal(param.Value);
                    if (param.Name.Equals("taxpercentage"))
                        company.TaxPercentage = Convert.ToDecimal(param.Value);
                    if (param.Name.Equals("transactionExpiration"))
                        company.TransactionDeleteExpiration = Convert.ToInt32(param.Value);
                    if (param.Name.Equals("suspend"))
                        company.PlatformSuspend = Convert.ToBoolean(param.Value);
                }

                stats = TransactionDA.GetStats(DateTime.Today);

                if (stats != null)
                {
                    company.Open = stats.Open;
                    company.Close = stats.Close;
                    company.Average = stats.Average;
                    company.Minimal = stats.Minimal;
                    company.Maximum = stats.Maximum;
                }
            }
            catch (Exception ex)
            {
                LogEntry log = new LogEntry();
                log.EventId = 100;
                log.Message = ex.Message;
                log.Categories.Add("GetCompanyParameters");
                log.Severity = TraceEventType.Error;
                log.Priority = 0;
                log.ProcessName = "GetCompanyParameters Method";
                Logger.Write(log);
            }

            return company;

        }
        public List<Metro.Admin.BusinessEntities.Order> GetOrdersFromUser(string idUser, DateTime date, string market)
        {
            List<Metro.Admin.BusinessEntities.Order> orders = null;
            Metro.Admin.DataAccess.Order orderDA = new Metro.Admin.DataAccess.Order();     

            try
            {
                orders = orderDA.GetOrdersFromUser(idUser,date,market);   
            }
            catch (Exception ex)
            {
                LogEntry log = new LogEntry();
                log.EventId = 100;
                log.Message = ex.Message;
                log.Categories.Add("GetOrdersFromUser");
                log.Severity = TraceEventType.Error;
                log.Priority = 0;
                log.ProcessName = "GetOrdersFromUser Method";
                Logger.Write(log);
            }
            return orders; 
        }
        public List<Metro.Admin.BusinessEntities.Transaction> GetTransactionsFromUser(string idUser,DateTime date, string market)
        {
            List<Metro.Admin.BusinessEntities.Transaction> transactions = null;
            Metro.Admin.DataAccess.Transaction transactionDA = new Metro.Admin.DataAccess.Transaction();     

            try
            {
                transactions = transactionDA.GetTransactionsFromUser(idUser,date,market); 
            }
            catch (Exception ex)
            {
                LogEntry log = new LogEntry();
                log.EventId = 100;
                log.Message = ex.Message;
                log.Categories.Add("GetTransactionsFromUser");
                log.Severity = TraceEventType.Error;
                log.Priority = 0;
                log.ProcessName = "GetTransactionsFromUser Method";
                Logger.Write(log);
            }

            return transactions; 
        }
    }
}
